package com.example.generator;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.enums.SqlLike;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.po.LikeTable;
import com.baomidou.mybatisplus.generator.config.querys.MySqlQuery;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;
import com.baomidou.mybatisplus.generator.fill.Property;
import com.baomidou.mybatisplus.generator.keywords.MySqlKeyWordsHandler;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * <pre>
 *     代码生成器
 * </pre>
 *
 * @author jiangtao
 * @version v1.0
 * @since 2019-01-10 12:07 AM
 */
public class MyBatisGenerator {

    private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
    private static final String HOST = "127.0.0.1";
    private static final String PORT = "3306";
    private static final String DB_NAME = "test_shop";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "root";
    private static final String DB_URL = "jdbc:mysql://" + HOST + ":" + PORT + "/" + DB_NAME + "?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai";

    /**
     * 演示
     */
    public static void main(String[] args) {
        String subProject = "/study-db/study-db-mybatis-plus-generator350";
        String basePackage = "com.example.module";
        String module = "sys";
        String pkgPath = System.getProperty("user.dir") + "/src/main/java";
        String pkgXml = System.getProperty("user.dir") + "/src/main/resource";

        String author = System.getProperty("user.name");
        String outputDir = "D:/code";

        FastAutoGenerator
                .create(new DataSourceConfig.Builder(DB_URL, USERNAME, PASSWORD)
                        .schema("")
                        .dbQuery(new MySqlQuery())
                        .typeConvert(new MySqlTypeConvert())
                        .keyWordsHandler(new MySqlKeyWordsHandler()))
                .globalConfig((scanner, builder) -> {
                    builder.author(author)
                            .outputDir(outputDir)
                            .fileOverride()   //是否覆盖已有文件  默认值：false即不设置
                            .disableOpenDir() //是否打开输出目录  默认值：true
                            //.enableKotlin()
                            //.enableSwagger()
                            .dateType(DateType.TIME_PACK)   //时间策略
                            .commentDate("yyyy-MM-dd");     //注释日期
                })
                .packageConfig((scanner, builder) -> {
                    //包配置
                    builder.parent(basePackage)
                            .moduleName(module)
                            .entity("entity")
                            .mapper("dao")
                            .xml("mapper")
                            .service("service")
                            .serviceImpl("service.impl")
                            .controller("controller")
                            .other("enums")
                            .pathInfo(new HashMap<OutputFile, String>() {{
                                //put(OutputFile.mapperXml, pkgXml);
                                //put(OutputFile.service, pkgPath);
                            }});
                })
                .strategyConfig(builder -> {
                    builder.enableCapitalMode()
                            .enableSkipView()
                            //.disableSqlFilter()
                            //.enableSchema()
                            .likeTable(new LikeTable("sys_", SqlLike.RIGHT))
                            //.notLikeTable(new LikeTable("sys_", SqlLike.LEFT))
                            //.addInclude("sys_admin")
                            //.addExclude("table1", "table2", "table3")
                            .addTablePrefix("t_", "sys_", "cms_")
                            //.addFieldPrefix("is", "") // 例如 is_show 映射成 show

                            .entityBuilder()
                            //.superClass(BaseEntity.class)
                            //.superClass("com.baomidou.base.BaseEntity")
                            //.disableSerialVersionUID()
                            //.enableColumnConstant()
                            .enableChainModel()
                            .enableLombok()
                            //.enableRemoveIsPrefix()
                            //.enableTableFieldAnnotation()
                            .enableActiveRecord()
                            .versionPropertyName("version")
                            .versionColumnName("version")
                            .logicDeletePropertyName("enable")
                            .logicDeleteColumnName("enable")
                            .naming(NamingStrategy.underline_to_camel)
                            .columnNaming(NamingStrategy.underline_to_camel)
                            //.addSuperEntityColumns("id", "created_by", "created_time", "updated_by", "updated_time", "version")
                            .addIgnoreColumns("enableName")
                            .addTableFills(new Column("enable", FieldFill.INSERT))
                            .addTableFills(new Column("create_time", FieldFill.INSERT))
                            .addTableFills(new Property("updateTime", FieldFill.INSERT_UPDATE))
                            .addTableFills(new Property("version", FieldFill.INSERT_UPDATE))
                            .idType(IdType.AUTO)
                            .formatFileName("%s")   //%sEntity

                            .mapperBuilder()
                            //.superClass(BaseMapper.class)
                            //.superClass("com.baomidou.global.BaseMapper")
                            .enableMapperAnnotation()
                            .enableBaseResultMap()
                            .enableBaseColumnList()
                            //.cache(MyMapperCache.class)
                            .formatMapperFileName("%sDao")
                            .formatXmlFileName("%sMapper")

                            .serviceBuilder()
                            //.superServiceClass(BaseService.class)
                            //.superServiceClass("com.baomidou.global.BaseService")
                            //.superServiceImplClass(BaseServiceImpl.class)
                            //.superServiceImplClass("com.baomidou.global.BaseServiceImpl")
                            .formatServiceFileName("I%sService")
                            .formatServiceImplFileName("%sServiceImp")

                            .controllerBuilder()
                            //.superClass(BaseController.class)
                            //.superClass("com.baomidou.global.BaseController")
                            //.enableHyphenStyle() //开启驼峰转连字符
                            .enableRestStyle()
                            .formatFileName("%sController");
                })
                .templateConfig(builder -> {
                    //模板配置
                    //builder.entity("src/main/resources/tmpl/entity.java.vm")
                    //.entityKt()
                    //.mapper()
                    //.mapperXml()
                    //.service()
                    //.controller()
                    //.disable()
                })
                .injectionConfig(builder -> {

                })
                .templateEngine(new VelocityTemplateEngine())
                .execute();
    }

}
